﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

namespace OfficeToolbox.SharePoint.Lists.FormSettings {
    public class FormFieldIterator : ListFieldIterator {
        Dictionary<string, Dictionary<string, string>> dictionary = null;

        protected override bool IsFieldExcluded(SPField field) {
            SPFeatureDefinition feature = SPContext.Current.Site.WebApplication.Farm.FeatureDefinitions[new Guid("6810C2DF-DA25-41c1-9CCE-1F966E7FC359")];
            if ((feature != null) && (feature.Status == SPObjectStatus.Online)) {
                if (!SPContext.Current.List.RootFolder.Properties.ContainsKey((object)"ListFormSettings")) {
                    return base.IsFieldExcluded(field);
                }
                dictionary = ConvertFromString((string)SPContext.Current.List.RootFolder.Properties[(object)"ListFormSettings"]);
                string str = string.Empty;
                if (!dictionary.ContainsKey(field.InternalName)) {
                    return base.IsFieldExcluded(field);
                }
                switch (SPContext.Current.FormContext.FormMode) {
                    case SPControlMode.Display:
                        str = dictionary[field.InternalName]["Display"];
                        if (!str.Equals("1")) {
                                return true;
                        }
                        return false;

                    case SPControlMode.Edit:
                        str = dictionary[field.InternalName]["Edit"];
                        if (!str.Equals("1")) {
                            return true;
                        }
                        return false;

                    case SPControlMode.New:
                        if (dictionary[field.InternalName].ContainsKey("New")) {
                            str = dictionary[field.InternalName]["New"];
                            if (!str.Equals("1")) {
                                return true;
                            }
                            return false;
                        }
                        return base.IsFieldExcluded(field);
                }
            }
            return base.IsFieldExcluded(field);
        }

        public static Dictionary<string, Dictionary<string, string>> ConvertFromString(string values) {
            Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>();
            foreach (string str in values.Split("#".ToCharArray())) {
                if (!string.IsNullOrEmpty(str)) {
                    string[] strArray2 = str.Split("|".ToCharArray());
                    string key = strArray2[0];
                    string str3 = strArray2[1];
                    string str4 = strArray2[2];
                    if (!dictionary.ContainsKey(key)) {
                        dictionary.Add(key, new Dictionary<string, string>());
                    }
                    dictionary[key].Add(str3, str4);
                }
            }
            return dictionary;
        }

        public static string ConvertToString(Dictionary<string, Dictionary<string, string>> data) {
            StringBuilder builder = new StringBuilder();
            foreach (string str in data.Keys) {
                foreach (string str2 in data[str].Keys) {
                    builder.Append(string.Format("{0}|{1}|{2}#", str, str2, data[str][str2]));
                }
            }
            return builder.ToString();
        }
    }
}